<template>
  <!-- 顶部标题区域 -->
  <div class="banner-header">
    <div class="banner-header-content">
      <div class="header-left">
        <div class="logo">
          <img src="../../../assets/images/logo.png" alt="logo">
        </div>
        <div class="header-info">
          <h1 class="main-title">西大数信数帮学堂</h1>
          <p class="sub-title">广西大学数学与信息科学学院</p>
        </div>
      </div>

      <!-- 中间导航按钮 -->
      <div class="header-nav-container">
        <!-- 左侧滚动箭头按钮 -->
        <button
          class="nav-arrow left"
          @click="scrollNav('left')"
          :disabled="currentIndex === 0"
        >
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
            <path d="M15 18L9 12L15 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>

        <!-- 导航项容器 -->
        <div class="header-nav-wrapper">
          <div class="header-nav" ref="navRef">
            <!-- 遍历渲染导航项 -->
            <div v-for="(i,index) in state" :key="index" class="nav-item">
              <miku-image :res-id="i.iconName" video class="w-16 course-icon" preview />
              <router-link class="channel-link" :to="i.url" target="_blank" :title="i.channelName">
                {{ i.channelName }}
              </router-link>
            </div>
          </div>
        </div>

        <!-- 右侧滚动箭头按钮 -->
        <button
          class="nav-arrow right"
          @click="scrollNav('right')"
          :disabled="state.length <= 8 || currentIndex >= totalPages - 1"
        >
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
            <path d="M9 18L15 12L9 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>
      </div>

      <!-- 右侧搜索框 -->
      <div class="header-right">
        <div class="search-box" style="display: flex;">
          <search-form>
            <template #prefix-icon>
              <button class="iconfont icon-24gl-search submit-ico" type="submit" tabindex="-1"></button>
            </template>
            <template #submit>
              <button class="submit" type="submit" tabindex="-1">搜索</button>
            </template>
          </search-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { listVideoChannels } from '@/apis/video/channel';
import SearchForm from '@/components/NavBar/search/SearchForm.vue'

/**
 * 异步获取视频频道列表数据
 */
const { state } = useAsyncState(listVideoChannels(), [])

/**
 * 导航容器的引用，用于控制滚动位置
 */
const navRef = ref<HTMLElement | null>(null)

/**
 * 当前导航滚动索引，控制横向滚动的位置
 */
const currentIndex = ref(0)
const totalPages = computed(() => {
  return Math.ceil(state.value.length / 3); // 每页8个项
});
/**
 * 滚动导航函数
 * @param direction 滚动方向，'left' 表示向左滚动，'right' 表示向右滚动
 *
 * 说明：
 * - 向左滚动时，将 currentIndex 减 1，最小为 0；
 * - 向右滚动时，将 currentIndex 加 1，最大值根据导航项数量计算；
 * - 使用 nextTick 确保 DOM 更新后再设置 transform 样式。
 */
const scrollNav = (direction: 'left' | 'right') => {
  if (direction === 'left') {
    currentIndex.value = Math.max(0, currentIndex.value - 1)
  } else {
    // currentIndex.value = Math.min(Math.ceil((state.value.length - 8) / 4), currentIndex.value + 1)
     currentIndex.value = Math.min(totalPages.value - 1, currentIndex.value + 1);
  }

  nextTick(() => {
    if (navRef.value) {
      navRef.value.style.transform = `translateX(-${currentIndex.value * 20}%)`
    }
  })
}
</script>

<style scoped lang="scss">
/* 图标样式 */
.course-icon {
  width: 2.875rem;
  height: 1.875rem;
  //margin-right: -1.5rem;
  border-radius: 20%;
  object-fit: cover;
}

.dynamic {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 12px;
  margin-right: 16px;
}

/* 顶部标题区域样式 */
.banner-header {
  margin-bottom: 20px;
}

.banner-header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.logo {
  width: 60px;
  height: 60px;
}

.logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.header-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.main-title {
  font-size: 24px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 0;
  letter-spacing: 1px;
}

.sub-title {
  font-size: 14px;
  color: #666;
  margin: 0;
  letter-spacing: 0.5px;
}

/* 导航容器 */
.header-nav-container {
  display: flex;
  align-items: center;
  flex: 1;
  margin: 0 20px;
  overflow: hidden;
}

.nav-arrow {
  background: #f5f5f5;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  margin: 0 8px;

  &:hover:not(:disabled) {
    background: #e0e0e0;
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}

.header-nav-wrapper {
  flex: 1;
  overflow: hidden;
  height: 100px; /* 两行的高度 */
}

.header-nav {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 10px;
  transition: transform 0.3s ease;
  width: max-content;
  grid-auto-flow: column;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 2px;
  width: 120px; /* 固定宽度 */
}

.channel-link {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
  border-radius: 8px;
  user-select: none;
  transition: all .2s;
  height: 36px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
  width: 100%;

  &:hover {
    background-color: var(--bg1);
  }
}

.header-nav div span {
  font-size: 12px;
  color: #fff;
  background-color: #24ccfd;
}

.header-right {
  width: 400px;
  height: 50px;
  z-index: 99999;
}

.search-box {
  .search-form {
    input.search-input:focus {
      /* border-bottom: 1px solid var(--bg2); */
    }

    .search-border {
      border-color: var(--bg2);

      .search-box {
        transition: background .3s;
        background-color: var(--bg1);
      }

      &:focus-within {
        border-color: var(--blue1);
        .search-content {
          background-color: unset;
        }
      }

      &:focus-within, &:hover {
        .search-box {
          background-color: unset;
        }
      }
    }

    button.submit {
      width: 50px;
      height: 30px;
      border-radius: 8px;
      background-color: var(--blue1);
      color: #fff;
      font-size: 16px;
      transition: all .3s;
      cursor: pointer;
      user-select: none;

      &:hover {
        background-color: var(--blue1-hover);
      }
    }

    .submit-ico {
      background: none;
      color: var(--blue1);
      font-weight: 800;
      padding-left: 4px;
      padding-right: 12px;
      cursor: pointer;
    }
  }
}
</style>
